package com.zte.shopping.action.backend;

import com.zte.shopping.constant.ResponseCodeConstant;
import com.zte.shopping.entity.ProductType;
import com.zte.shopping.entity.User;
import com.zte.shopping.exception.RequestParameterException;
import com.zte.shopping.service.UserService;
import com.zte.shopping.util.PageSupport;
import com.zte.shopping.util.ResponseResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: ssm
 * @description: 后台用户管理界面
 * @author: zuti
 * @create: 2020-06-23 22:11
 **/
@Controller
@RequestMapping("user")
public class BackendUserManagerController {

    @Autowired
    UserService userService;

    private  static  final Logger logger= Logger.getLogger(BackendUserManagerController.class);

    @RequestMapping(value = "/toUser")
    public String  toUserManager(@RequestParam(value = "pageIndex",required = false,defaultValue = "1")Integer pageIndex,
                                 Model model){
        logger.debug("enterBackendUserManagerController------> toUserManager param: pageIndex"+pageIndex);
        List<User> userList = userService.userList(pageIndex);
        logger.debug("enterBackendUserManagerController------> toUserManager UserList : size" + userList.size());
        for(int i=0;i<userList.size();i++) {
            User user = userList.get(i);
            logger.debug("userId="+user.getUserId()+" LoginName="+user.getLoginName()+" address="+user.getAddress()+" phone="+user.getPhone()
            +" date="+user.getRegistDate());
        }
        PageSupport pageSupport = new PageSupport();
        pageSupport.setTotalCount(this.userService.userListCount());
        pageSupport.setCurrentPageNo(pageIndex);

        model.addAttribute("pageSupport", pageSupport);
        model.addAttribute("UserList", userList);

        return "backend/userManager";
    }


    /**
     * @Author zuti
     * @Description //TODO zuti666
     * @Date 20:02 2020/6/28
     * @Param [pageIndex, UserName, LoginName, Phone, Address, IsValid, request, model]
     * @return java.lang.String
     **/
    @RequestMapping(value = "/searchByParamList")
    public String  UserManagerSearch(@RequestParam(value = "pageIndex",required = false,defaultValue = "1")Integer pageIndex,
                                     @RequestParam(value = "searchUserName",required = false)  String UserName,
                                     @RequestParam(value = "searchLoginName",required = false)  String LoginName,
                                     @RequestParam(value = "searchPhone",required = false)  String Phone,
                                     @RequestParam(value = "searchAddress",required = false)  String Address,
                                     @RequestParam(value = "searchIsValid",required = false)  Integer IsValid,
                                 Model model){

        logger.debug("enter UserManagerSearch------> UserManagerSearch param: pageIndex="+pageIndex+"searchName="+UserName+
                " searchLoginName="+LoginName+" searchPhone="+Phone+" searchAddress="+Address+" searchIsValid"+IsValid);
        if(IsValid!=null){
            if(IsValid == -1){
            IsValid=null;
            }
        }
        List<User> userList = userService.searchUserByParmaList(pageIndex,UserName,LoginName,Phone,Address,IsValid);


        logger.debug("enter UserManagerSearch------> toUserManager UserList : size" + userList.size());
        for(int i=0;i<userList.size();i++) {
            User user = userList.get(i);
            logger.debug("userId="+user.getUserId()+" LoginName="+user.getLoginName()+" address="+user.getAddress()+" phone="+user.getPhone()
                    +" date="+user.getRegistDate());
        }

        PageSupport pageSupport = new PageSupport();
        pageSupport.setTotalCount(this.userService.userListCountByParamList(UserName,LoginName,Phone,Address,IsValid));
        pageSupport.setCurrentPageNo(pageIndex);

        model.addAttribute("pageSupport", pageSupport);
        model.addAttribute("UserList", userList);

        model.addAttribute("UserName",UserName);
        model.addAttribute("LoginName",LoginName);
        model.addAttribute("Phone",Phone);
        model.addAttribute("Address",Address);
        model.addAttribute("IsValid",IsValid);


        return "backend/userManager";
    }


    /**
     * @Author zuti
     * @Description //TODO 修改用户状态
     * @Date 22:56 2020/6/28
     * @Param [id, status]
     * @return com.zte.shopping.util.ResponseResult
     **/
    @ResponseBody
    @RequestMapping(value = "modifyStatus",method = RequestMethod.POST)
    public ResponseResult modifyStatus(@RequestParam("id") String id,
                                       @RequestParam("status") String status){

        ResponseResult result = new ResponseResult();

        try
        {
            userService.modifyStatus(id, status);

            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);

        } catch (RequestParameterException e)
        {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (Exception e)
        {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }

        logger.debug("BackendUserManagerController==============> modifyStatus params:id="+id+" status="+status+"  =====result: Message"
            +result.getMessage()+" result.ResponseCode="+result.getResponseCode());

        return result;
    }


    /**
     * @Author zuti
     * @Description //TODO 后台修改用户信息
     * @Date 22:57 2020/6/28
     * @Param [user]
     * @return com.zte.shopping.util.ResponseResult
     **/
    @ResponseBody
    @RequestMapping("/modifyById")
    public ResponseResult modifyById(User user)
    {
        ResponseResult result = new ResponseResult();


        try {
            userService.modifyById(user);

            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);

//        } catch (UserExistException e)
//        {
//            result.setMessage(e.getMessage());
//            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
//
//        }
        }catch (Exception e)
        {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        logger.debug("BackendUserManagerController==============> modifyStatus params:userId="+user.getUserId()+" userName="+user.getUserName()+"  =====result: Message"
                +result.getMessage()+" result.ResponseCode="+result.getResponseCode());

        return result;
    }



}
